﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UCore
{
    public class FCom
    {

        private const string dir_io_file = "C:\\nut\\KUI-Code\\twin\\twin\\qwave\\qwave";
        private const string fn_from_term = "from_term_uart";
        private const string fn_from_console = "from_console_uart";

        public ConcurrentQueue<string> queueMsgOut = new ConcurrentQueue<string>();
        public ConcurrentQueue<string> queueMsgIn = new ConcurrentQueue<string>();


        public FCom() {
            threadsForRx();
        }


        public bool TryDequeue(out string message)
        {
            return queueMsgIn.TryDequeue(out message);
        }


        private void Enqueue(string[] inst)
        {
            //queueMsgIn.Clear();
            for (int i = 0; i < inst.Length; i++)
            {
                if (inst[i].Trim().Equals("") == false)
                {
                    queueMsgIn.Enqueue(inst[i]);
                }
            }
        }






        private void fSerial_in(object threadCOM)
        {
            // 在新线程中执行的代码
            //Console.WriteLine($"New thread {i} started");
            int i = (int)threadCOM + 1;
            string fname = dir_io_file + "\\" + fn_from_term + i.ToString() + ".txt";

            long fileSize;
            try
            {
                fileSize = new FileInfo(fname).Length;
            }
            catch (Exception ex)
            {
                Console.WriteLine(fname + " " + ex.Message);
                return;
            }

            long old_length = fileSize;
            string s;

            while (true) // 无限循环
            {
                try
                {
                    fileSize = new FileInfo(fname).Length;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    continue;
                }



                if (fileSize > 0)
                {
                    if (fileSize == old_length)
                    {
                        try
                        {
                            // 读取文件的所有行
                            //string[] lines = File.ReadAllLines(fname);
                            s = File.ReadAllText(fname);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            continue;
                        }

                        // 文件清空
                        try
                        {
                            File.WriteAllText(fname, "");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            continue;
                        }

                        //processMsgs(s, i);
                    }
                    else //不相等, 继续等待
                    {
                        old_length = fileSize;
                    }

                    // 等待100毫秒
                    Thread.Sleep(100);
                }
                else
                {
                    // 文件为空，等待1秒
                    Console.WriteLine("File is empty, waiting...");
                    Thread.Sleep(1000);
                }
            }
        }


        private void threadsForRx()
        {
            // 创建一个线程数组
            int threadCount = 5;
            Thread[] threadCom = new Thread[threadCount];

            // 创建并启动线程
            for (int i = 0; i < threadCount; i++)
            {
                threadCom[i] = new Thread(new ParameterizedThreadStart(fSerial_in));
                threadCom[i].Start(i); // 传递线程索引作为参数
            }

        }



    }
}
